package adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import bean.People;
import bean.Students;
import student.andlei.com.student.R;

public class ExtendListviewAdapter extends BaseExpandableListAdapter {

	private List<People> pList = null;
	private LayoutInflater inflater = null;
	private Context context;
	public ExtendListviewAdapter(Context context, List<People> parent) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.pList = parent;
	}
	public interface Myonclick{
		void onMyclick(View view,int i,int l);


	}
	private Myonclick myonclick,myonclick2;

	public Myonclick getMyonclick2() {
		return myonclick2;
	}

	public void setMyonclick2(Myonclick myonclick2) {
		this.myonclick2 = myonclick2;
	}

	public void setMyonclick(Myonclick myonclick) {
		this.myonclick = myonclick;
	}

	// 得到子item需要关联的数据
	@Override
	public Students getChild(int groupPosition, int childPosition) {
		People key = pList.get(groupPosition);
		return (key.getStudents().get(childPosition));
	}

	// 得到子item的ID
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// 设置子item的组件
	@Override
	public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
							 ViewGroup parent) {
		CViewHolder cholder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.txtitem, null);
			cholder = new CViewHolder();
			cholder.rlayout = convertView.findViewById(R.id.rlayout_);
			cholder.tv = (TextView) convertView.findViewById(R.id.items);
			cholder.btn = (ImageView) convertView.findViewById(R.id.btns);
			convertView.setTag(cholder);
		} else {
			cholder = (CViewHolder) convertView.getTag();
		}

		final People key = pList.get(groupPosition);
		Students info = key.getStudents().get(childPosition);
		cholder.tv.setText(info.getJob());
		final CViewHolder finalCholder = cholder;
		if(info.isSelect()){
			cholder.tv.setTextColor(0xFFFFFFFF);
			cholder.btn.setImageResource(R.mipmap.guanbibai);
			cholder.rlayout.setBackgroundResource(R.color.homepage_titlefont);
		}else {
			cholder.tv.setTextColor(0xFF333333);
			cholder.btn.setImageResource(R.mipmap.guanbihui);
			cholder.rlayout.setBackgroundResource(R.color.appstyletitlecolor);
		}
		if(groupPosition==1){
			cholder.btn.setVisibility(View.GONE);
		}else {
			cholder.btn.setVisibility(View.VISIBLE);
		}
		if(info.getJob().equals("课堂直播笔记")){
			cholder.btn.setVisibility(View.GONE);
		}else {
			cholder.btn.setVisibility(View.VISIBLE);
		}

		cholder.btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(myonclick!=null) {
					myonclick.onMyclick(finalCholder.btn,groupPosition,childPosition);
				}
			}
		});





		return convertView;
	}

	// 获取当前父item下的子item的个数
	@Override
	public int getChildrenCount(int groupPosition) {
		People key = pList.get(groupPosition);
		if(key.getStudents()==null)return 0;
		int size = key.getStudents().size();
		return size;
	}

	// 获取当前父item的数据
	@Override
	public People getGroup(int groupPosition) {
		return pList.get(groupPosition);
	}

	// 获取当前父item的个数
	@Override
	public int getGroupCount() {
		return pList.size();
	}

	// 得到当前父item的ID
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	// 设置父item组件
	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@Override
	public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GViewHolder gholder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.students, null);
			gholder = new GViewHolder();
			gholder.tv = (TextView) convertView.findViewById(R.id.txt1);
			gholder.iv = (ImageView) convertView.findViewById(R.id.image1);
			gholder.iv1 = (ImageView) convertView.findViewById(R.id.image2);
			convertView.setTag(gholder);
		} else {
			gholder = (GViewHolder) convertView.getTag();
		}
		People p = pList.get(groupPosition);
		gholder.tv.setText("  "+p.getName());
		final GViewHolder finalGholder = gholder;
		gholder.iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if(myonclick2!=null) {
					myonclick2.onMyclick(finalGholder.iv,groupPosition,0);
				}
			}
		});
		if (!isExpanded) {
			gholder.iv1.setImageResource(R.mipmap.fenlei);
		} else {
			gholder.iv1.setImageResource(R.mipmap.xuanze);
		}
		if(groupPosition == 0){
			gholder.iv1.setVisibility(View.VISIBLE);
			gholder.iv.setImageResource(R.mipmap.tianjia);
            Drawable drawable = ContextCompat.getDrawable(context.getApplicationContext(),R.mipmap.biji);
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            gholder.tv.setCompoundDrawables(drawable, null, null, null);
		}else if(groupPosition == 1){
			gholder.iv1.setVisibility(View.GONE);
			gholder.iv.setImageResource(R.mipmap.gengduo);
            Drawable drawable = ContextCompat.getDrawable(context.getApplicationContext(),R.mipmap.icon_biji_sahnchu);
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            gholder.tv.setCompoundDrawables(drawable, null, null, null);
		}

//		if (p.getSex() == 1) {
//			gholder.iv.setImageResource(R.mipmap.qqweibo_logo);
//		} else if (p.getSex() == 2) {
//			gholder.iv.setImageResource(R.mipmap.sinalogo_small);
//		} else if (p.getSex() == 3) {
//			gholder.iv.setImageResource(R.mipmap.qqzone_logo);
//		}


		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public class GViewHolder {
		private TextView tv;
		private ImageView iv;
		private ImageView iv1;
	}

	public class CViewHolder {
		private TextView tv;
		private ImageView btn;
		private RelativeLayout rlayout;
	}

}
